﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GENERIC.Respository.Interfaces;
using System.Data.Linq;
using System.Transactions;

namespace GENERIC.LinqToSqlRepository
{
    public class LinqToSqlContext : IContext
    {
        #region Constructeur

        public LinqToSqlContext(DataContext context)
        {
            Context = context;
        }

        #endregion

        #region Properties

        private DataContext Context { get; set; }

        #endregion
        
        
        public IGENERICSet<TEntity> CreateSet<TEntity>() where TEntity : class, new()
        {
            return new LinqToSqlSet<TEntity>(Context.GetTable<TEntity>());
        }

        public void SaveChanges()
        {
            Context.SubmitChanges();
        }

        public void Dispose()
        {
            if (Context != null)
            {
                Context.Dispose();
            }
        }


        public ITransaction BeginTransaction()
        {
            return new GENERIC.LinqToSqlRepository.Transaction();
        }
    }
}
